package com.jiangjiang.eshop.commodity.controller;

import com.google.common.collect.Lists;
import com.jiangjiang.eshop.commodity.service.GoodsPictureService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * @ClassName GoodsPictureController
 * @Description 商品图品管理
 * @Author jhw
 * @Date 2021/8/2 16:13
 * @Version 1.0
 **/
@Slf4j
@RestController
@RequestMapping("/commodity/goods/picture")
public class GoodsPictureController {
    @Autowired
    private GoodsPictureService goodsPictureService;

    /**
     * 批量上传商品图品啊
     *
     * @param goodsId  商品id
     * @param pictures 图片集合
     * @return 上传结果
     */
    @PostMapping("/batchUpload")
    public Boolean batchUpload(Long goodsId, MultipartFile[] pictures) {
        try {
            return goodsPictureService.batchUpload(goodsId, pictures);
        } catch (Exception e) {
            log.warn("error", e);
        }
        return false;

    }

    /**
     * 根据商品id查询商品图片路径集合
     *
     * @param goodsId 商品id
     * @return 商品图片路径集合
     */
    @GetMapping("/{goodsId}")
    public List<String> queryPicUrlByGoodsId(@PathVariable("goodsId") Long goodsId) {
        try {
            return goodsPictureService.queryPicUrlByGoodsId(goodsId);
        } catch (Exception e) {
            log.error("error", e);
        }
        return Lists.newArrayList();
    }

    /**
     * 根据商品id删除图片
     *
     * @param goodsId 商品id
     */
    @PostMapping("/removeByGoodsId")
    public Boolean batchRemoveByGoodsId(@RequestParam("goodsId") Long goodsId) {
        try {
            return goodsPictureService.batchRemoveByGoodsId(goodsId);
        } catch (Exception e) {
            log.error("error", e);
            return false;
        }
    }

}
